< previous page page_153 next page >

Page 153
The first step in using the CommonDialog control to handle font selection is to set a value for the Flags property. Among other things, this property tells the CommonDialog control whether you want to show screen fonts, printer fonts, or both. This setting is required; the constants are listed in the following table:
Font Set
Constant
Value
Screen fonts
cdlCFScreenFonts
1
Printer fonts
cdlCFPrinterFonts
2
Both sets
cdlCFBoth
3

CAUTION
If you do not set a value for the Flags property, you get an error message stating that no fonts are installed.

You can set the value of the Flags property from the design environment by using the Properties window or the Property Pages dialog box, or you can set it from your program by using an assignment statement. After the Flags property has been set, you can invoke the Font dialog box from your code, using the CommonDialog's ShowFont method. After the dialog box is closed, information about the font that the user selected is contained in the CommonDialog's properties.
To see this process in action, follow these steps to modify the sample program you created earlier in this chapter:
1. If the program is still running from an earlier test, stop it.
2. Add a Label control to Form1. Set the Label control's Name property to lblTest and its Caption property to This is a test.
3. Set lblTest's AutoSize property to True.
4. Replace the code in cmdTest's Click event procedure with the following:
cdlTest.Flags = cdlCFBoth + cdlCFEffects
cdlTest.ShowFont
lblTest.Font.Name = cdlTest.FontName
lblTest.Font.Size = cdlTest.FontSize
lblTest.Font.Bold = cdlTest.FontBold
5. Run the application. When you click cmdTest, a Font dialog box should appear. Change the font to something totally different than the default. When you click OK, lblTest's font should match what you selected in the dialog box.
Note two things in the preceding code. First, an extra flag, cdlCFEffects, allows the user to select additional font "effects" such as bold, color, and underline. Second, note the slight difference in syntax between the Label control's Font properties and the CommonDialog's Font properties. The Font property of a Label control (or of other objects, such as a TextBox control) is an object itself (a Font object), and the CommonDialog box stores each attribute about a font (Name, Size, Bold, and so on) in a separate property. The text box has the older, separate

 
< previous page page_153 next page >